package openGl;

import android.opengl.GLES20;
import android.opengl.GLSurfaceView;
import android.opengl.Matrix;
import android.util.Log;

import java.nio.FloatBuffer;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import camera.OpenGl.utils.ByteBufferUtils;

/**
 * Created by Administrator on 2017/12/25.
 */

public class OpenGL20Render implements GLSurfaceView.Renderer {
    private String Tag="OpenGL20Render";
    private int glCreateProgram;
    private int mMatrixHandler;

    public OpenGL20Render() {
        triangleBytebuffer= ByteBufferUtils.makeFloteByteBuffer(triangleCoords);
        //初始化操作
       
    }

    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        //设置背景颜色
        GLES20.glClearColor(0.5f,0.5f,0.5f,1.0f);
        //加载顶点着色器
        int vertexShader = OpenGlUtils.loadShader(GLES20.GL_VERTEX_SHADER, OpenGlUtils.vertexShaderCode);
        if (vertexShader==0){
            Log.e(Tag,"加载vertexShader失败");
        }
        //加载片元着色器
        int fragmentShader = OpenGlUtils.loadShader(GLES20.GL_FRAGMENT_SHADER, OpenGlUtils.fragmentShaderCode);
        if (fragmentShader==0){
            Log.e(Tag,"fragmentShader失败");
        }
        //创建OpenGLES程序
        glCreateProgram = GLES20.glCreateProgram();
        //将顶点着色器加入到程序中
        GLES20.glAttachShader(glCreateProgram,vertexShader);
        //将片元着色器加入到程序中
        GLES20.glAttachShader(glCreateProgram,fragmentShader);
        //连接着色器程序
        GLES20.glLinkProgram(glCreateProgram);
    }

    @Override
    public void onSurfaceChanged(GL10 gl, int width, int height) {
        //设置窗体大小
        GLES20.glViewport(0,0,width,height);
        //计算宽高比
        float ratio=(float)width/height;
        //设置透视投影
        Matrix.frustumM(mProjectMatrix, 0, -ratio, ratio, -1, 1, 3, 7);
        //设置相机位置
        Matrix.setLookAtM(mViewMatrix, 0, 0, 0, 7.0f, 0f, 0f, 0f, 0f, 1.0f, 0.0f);
        //计算变换矩阵
        Matrix.multiplyMM(mMVPMatrix,0,mProjectMatrix,0,mViewMatrix,0);
    }

    @Override
    public void onDrawFrame(GL10 gl) {
        GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT|GLES20.GL_DEPTH_BUFFER_BIT);
        //将主程序加入到OpenGLES2.0的环境中
        GLES20.glUseProgram(glCreateProgram);
        //获取变换矩阵vMatrix成员句柄
        mMatrixHandler= GLES20.glGetUniformLocation(glCreateProgram,"vMatrix");
        //指定vMatrix的值
        GLES20.glUniformMatrix4fv(mMatrixHandler,1,false,mMVPMatrix,0);
        //获取顶点着色器vPosition的句柄
        int vPositionHandle = GLES20.glGetAttribLocation(glCreateProgram, "vPosition");
        //设置该句柄可用
        GLES20.glEnableVertexAttribArray(vPositionHandle);
      
        //设置三角形坐标数据
        GLES20.glVertexAttribPointer(vPositionHandle,
                3,
                GLES20.GL_FLOAT,
                false,
                0,
                triangleBytebuffer);
        //获取片元着色器的句柄
        int vColor = GLES20.glGetAttribLocation(glCreateProgram, "vColor");
        GLES20.glEnableVertexAttribArray(vColor);
        //设置三角形的颜色
        GLES20.glUniform4fv(vColor, 1, colorArray, 0);
        //绘制三角形
        GLES20.glDrawArrays(GLES20.GL_TRIANGLES,0,3);
        //设置顶点不可用
        GLES20.glDisableVertexAttribArray(vPositionHandle);
    }
    
    //////////////////////////////////
    /***
     * 绘制三角形
     */
    private FloatBuffer triangleBytebuffer;
    float color[] = { 0.5f, 1.0f, 1.0f, 1.0f }; //白色
    float triangleCoords[] = {
            0.5f,  0.5f, 0.0f, 
            -0.5f, -0.5f, 0.0f,
            0.5f, -0.5f, 0.0f 
    };
    private float[] colorArray=new float[]{
            1,0,0,
            1,1,0,
            1,0,1,
            0,0,1
    };
    /**
     * 绘制等腰直角三角形
     */
    private float[] mViewMatrix=new float[16];
    private float[] mProjectMatrix=new float[16];
    private float[] mMVPMatrix=new float[16];
}
